package com.logansoft.zhxypkoa.domain.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import javax.validation.constraints.NotNull;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.logansoft.zhxypkoa.domain.Base;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户模块
 * 
 * @author zhuyj
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "s_sys_user")
@Scope(value = "prototype")
@JsonIgnoreProperties(ignoreUnknown = true, value = { "password" })
public class User extends Base {

	private static final long serialVersionUID = 1L;

	/**
	 * 用户名
	 */
	@NotEmpty(message = "用户名不能为空")
	@Size(min = 5, max = 32, message = "用户名长度为5-32")
	// @Pattern(regexp="[0-9a-zA-Z]{6-20}", message="用户名长度必须为6-20, 不包含特殊字符")
	@NotNull(message = "用户名不能为空")
	@Column(name = "USER_NAME", length = 32, unique = true)
	private String username;
	/**
	 * 原始密码, 用于 mvc 注入, 不保存数据库
	 */
	@Transient
	 @NotEmpty(message="密码不能为空")
	@Size(min = 6, max = 20, message = "密码长度为6-20")
	@NotNull(message = "密码不能为空")
	// @Pattern(regexp="[0-9a-zA-Z]{6-20}", message="密码长度必须为6-20, 不包含特殊字符")
	@Column(name = "ORG_PASSWORD", length = 128)
	private String orgPassword;
	/**
	 * 编码后的密码
	 */
	@Column(name = "PASSWORD", length = 512)
	private String password;
	/**
	 * 性别
	 */
	@Column(name = "SEXE", columnDefinition = "tinyint(1) DEFAULT 0 COMMENT '未知'")
	private int sexe;
	/**
	 * 年龄
	 */
	@Column(name = "AGE", columnDefinition = "tinyint DEFAULT 0 COMMENT '年龄'")
	private int age;
	/**
	 * 电话
	 */
	@Column(name = "PHONE", length = 16)
	private String phone;
	/**
	 * 地址
	 */
	@Column(name = "ADDRESS", length = 128)
	private String address;
	/**
	 * 邮箱
	 */
	@Column(name = "EMAIL", length = 128)
	private String email;
	/**
	 * 角色组
	 */
	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Role.class)
	@JoinTable(name = "s_sys_user_role", joinColumns = {
			@JoinColumn(name = "USER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
					@JoinColumn(name = "ROLE_ID", referencedColumnName = "ID") })
	private List<Role> roles;
	/**
	 * 组织组
	 */
	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Organization.class)
	@JoinTable(name = "s_sys_user_organization", joinColumns = {
			@JoinColumn(name = "USER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
					@JoinColumn(name = "ORGANIZATION_ID", referencedColumnName = "ID") })
	private List<Organization> organizations;
	/**
	 * 密码更改时间
	 */
	@Column(name = "LAST_PASSWORD_RESET_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date lastPasswordResetTime;
	/**
	 * 头像
	 */
	@Column(name = "IMAGE_URL", length = 1024)
	private String imageUrl;
	
	public User() {
		// TODO Auto-generated constructor stub
	}

	public User(String id) {
		this.id = id;
	}

	public User(String id, String name) {
		this.id = id;
		this.name = name;
	}

	@Transient
	public List<String> getRoleIdList() {
		List<String> list = new ArrayList<String>();
		if (roles != null) {
			for (Role role : roles) {
				list.add(role.getId());
			}
		}
		return list;
	}

	@Transient
	public List<String> getRoleCodeList() {
		List<String> list = new ArrayList<String>();
		if (roles != null) {
			for (Role role : roles) {
				list.add(role.getCode());
			}
		}
		return list;
	}

	@Transient
	public String getSexStr() {
		switch (this.sexe) {
		case 0:
			return "未知";
		case 1:
			return "男";

		default:
			return "女";
		}
	}
}
